﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TianYi
{

    public class PlcToPc
    {
        #region 读取PLC地址解析
        //plc中类型与c#类型 bool => Bit
        //Byte => byte
        //word => ushort
        //DWord => uint
        //Int => short
        //DInt => int
        //Real => float
        //LReal => double
        //String => string
        //DateTimeLong=>datetime
        //s7wstring=>string
        //ReadTimeout,WriteTimeout,分别为读取和写入等待时间
        #endregion
        PlcController _plc;

        public PlcToPc(PlcController plcInstance)
        {
            this._plc = plcInstance;
        }
        //请求加工信号
        private int _uploadStar;         //总站请求
        private int _uploadStarStation1; //第一站请求
        private int _uploadStarStation2; //第二站请求

        //条码数据读取
        private string _sn_ChannelOP10A;
        private string _sn_ChannelOP10B;
        //数据读取
        private double _pressureValue;  //压力
        private double _pressurelimitValue;  //压力极限值


        private double _production_length;  //长度
        private double _large_out_diameter;  //大外径
        private double _small_out_diameter;  //小外径
        private double _bottom_cover_value;  //底盖外径
        private double _production_height;  //高度
        private double _displacementValue;  //位移值
        private string _qr_code;           //二维码

        //给PLC给上位机完成信号
        private short _downloadDoneSign;


        //plc心跳信号
        private short PCwatchDog10;

        public short WatchDog10()
        {
            PCwatchDog10 = (short)_plc.ReadInt("DB1001.DBW8");
            return PCwatchDog10;
        }


        #region 读取各个工站扫码信号

        /// <summary>
        /// 请求加工总信号
        /// </summary>
        public int UploadStar
        {
            get
            {
                _uploadStar = _plc.ReadInt("DB1000.DBW2");
                return _uploadStar;
            }
            set
            {
                _uploadStar = value;
            }
        }
        /// <summary>
        /// 请求加工第一站
        /// </summary>
        public int UploadStartStation1
        {
            get
            {
                _uploadStarStation1 = _plc.ReadInt("DB1000.DBW6");//需要分开
                return _uploadStarStation1;
            }
            set
            {
                _uploadStarStation1 = value;
            }
        }
        /// <summary>
        /// 请求加工第二站
        /// </summary>
        public int UploadStartStation2
        {
            get
            {
                _uploadStarStation2 = _plc.ReadInt("DB1000.DBW10");
                return _uploadStarStation2;
            }
            set
            {
                _uploadStarStation2 = value;
            }
        }
        #endregion

        #region 工站读码地址
        /// <summary>
        /// 读取产品码
        /// </summary>
        public string Sn_ChannelOP10A()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1000, 16, 40);//第一个字节最大字节，第二个字节当前字节
            if (buffer != null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP10A = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP10A;
            }
            return null;
        }
        /// <summary>
        ///  读取产品码
        /// </summary>
        public string Sn_ChannelOP10B()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1001, 238, 40);
            if (buffer != null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP10B = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP10B;
            }
            return null;
        }
        #endregion

        #region 读取各个工站数据
        /// <summary>
        /// 读取产品长度
        /// </summary>
        /// <returns></returns>
        public double ProductionLength()
        {
            _production_length = _plc.ReadDouble(1000, 58, 1);
            if (_production_length != -999.999)
            {
                return _production_length;
            }
            return default;
        }

        /// <summary>
        /// 读取大外径
        /// </summary>
        /// <returns></returns>
        public double LargOutDiamter()
        {
            _large_out_diameter = _plc.ReadDouble(1000, 62, 1);// 读取real类型
            if (_large_out_diameter != -999.999)
            {
                return _large_out_diameter;
            }
            return default;
        }

        /// <summary>
        /// 读取小外径
        /// </summary>
        /// <returns></returns>
        public double SmallOutDiamter()
        {
            _small_out_diameter = _plc.ReadDouble(1000, 66, 1);
            if (_small_out_diameter != -999.999)
            {
                return _small_out_diameter;
            }
            return default;
        }

        /// <summary>
        /// 读取底盖外径
        /// </summary>
        /// <returns></returns>
        public double BottomCoverValue()
        {
            _bottom_cover_value = _plc.ReadDouble(1000, 70, 1);
            if (_bottom_cover_value != -999.999)
            {
                return _bottom_cover_value;
            }
            return default;
        }

        /// <summary>
        /// 读取产品高度
        /// </summary>
        /// <returns></returns>
        public double ProductionHeight()
        {
            _production_height = _plc.ReadDouble(1000, 74, 1);
            if (_production_height != -999.999)
            {
                return _production_height;
            }
            return default;
        }

        /// <summary>
        /// 读取压力极限值
        /// </summary>
        /// <returns></returns>
        public double PressureLimitValue()
        {
            _pressurelimitValue = _plc.ReadDouble(1000, 78, 1);
            if (_pressurelimitValue != -999.999)
            {
                return _pressurelimitValue;
            }
            return default;
        }

        /// <summary>
        /// 读取产品位移值
        /// </summary>
        /// <returns></returns>
        public double DisplacementValue()
        {
            _displacementValue = _plc.ReadDouble(1000, 82, 1);
            if (_displacementValue != -999.999)
            {
                return _displacementValue;
            }
            return default;
        }
        /// <summary>
        /// 读取压力值实时压力
        /// </summary>
        /// <returns></returns>
        public double PressureValue()
        {
            _pressureValue = _plc.ReadDouble(1000, 86, 1);
            if (_pressureValue != -999.999)
            {
                return _pressureValue;
            }
            return default;
        }
        /// <summary>
        /// 读取二维码
        /// </summary>
        /// <returns></returns>
        public string QrCode()
        {
            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1000, 90, 48);
            if (buffer != null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _qr_code = _plc.GetPlcDataString(buffer, length + 2);
                return _qr_code;
            }
            return null;
        }

        #endregion

        #region  读取压力测试完成信号
        /// <summary>
        /// 读取压力测试完成信号
        /// </summary>
        public int UploadDonePressureSign()
        {

            _downloadDoneSign = (short)_plc.ReadInt("DB1000.DBW14");
            return _downloadDoneSign;

        }

        ///// <summary>
        ///// 完成信号工站1
        ///// </summary>
        //public int UploadDoneSignStation1()
        //{

        //    _downloadDoneSignStation1 = (short)_plc.ReadInt("DB1001.DBW10");
        //    return _downloadDoneSignStation1;

        //}
        ///// <summary>
        ///// 完成信号工站2
        ///// </summary>
        //public int UploadDoneStation2()
        //{
        //    _downloadDoneSignStation2 = (short)_plc.ReadInt("DB1001.DBW10");
        //    return _downloadDoneSignStation2;

        //}
        #endregion
    }
}
